﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Eflong.Config;
using Eflong.MonitoringLib.MonitoringUnit;

namespace KWBridge
{
    public partial class LimitForm : Form
    {
        public LimitForm( )
        {
            InitializeComponent( );
        }

        private LimitSetConfiguration LimitSetConfig
        {
            get { return LimitSetConfigurationManager.Config; }
        }

        private void LimitForm_Load( object sender , EventArgs e )
        {
            IEnumerable<DevType> enabledDevs = LimitSetConfig.DevTypes.Where( n => n.Enabled );
            foreach ( DevType type in enabledDevs )
            {
                cbDevType.Items.Add( type.Name );
                cbDevType2.Items.Add( type.Name );
            }
            cbDevType.Items.Insert( 0 , string.Empty );

            BaseCoalMineUnit coalMineUnit = CoalMineUnitManager.Instance( ).GetCoalMineUnit( KWBridgeConfig.Instance.Id );
            if ( coalMineUnit != null )
            {
                List<BaseMonitoringUnit> mointors = coalMineUnit.GetNoDeletesMonitoringUnits( );
                mointors.ForEach( n => cbDevID.Items.Add( n.m_strId ) );
                var units = from m in mointors where !string.IsNullOrEmpty( m.m_strUnit ) select new { Unit = m.m_strUnit };
                units.Distinct( ).ToList( ).ForEach( n => cbCurUnit.Items.Add( n.Unit ) );
                cbCurUnit.Items.Insert( 0 , string.Empty );
            }
        }

        private void btnSaveLimitSet_Click( object sender , EventArgs e )
        {
            if ( string.IsNullOrEmpty( cbDevType.Text ) )
            {
                MessageBox.Show( "请选择测点类型！" );
                cbDevType.Select( );
                return;
            }
            float meteUpper = 0f , meteLow = 0f , alertUpper = 0f , alertLow = 0f;
            if ( !string.IsNullOrEmpty( txtLCXX.Text ) && !float.TryParse( txtLCSX.Text , out  meteUpper ) )
            {
                MessageBox.Show( "量程上限仅支持浮点类型，请重新输入！" );
                txtLCSX.Focus( );
                return;
            }
            if ( !string.IsNullOrEmpty( txtLCXX.Text ) && !float.TryParse( txtLCXX.Text , out  meteLow ) )
            {
                MessageBox.Show( "量程下限仅支持浮点类型，请重新输入！" );
                txtLCXX.Focus( );
                return;
            }
            if ( !string.IsNullOrEmpty( txtBJSX.Text ) && !float.TryParse( txtBJSX.Text , out  alertUpper ) )
            {
                MessageBox.Show( "报警上限仅支持浮点类型，请重新输入！" );
                txtBJSX.Focus( );
                return;
            }
            if ( !string.IsNullOrEmpty( txtBJXX.Text ) && !float.TryParse( txtBJXX.Text , out  alertLow ) )
            {
                MessageBox.Show( "报警下限仅支持浮点类型，请重新输入！" );
                txtBJXX.Focus( );
                return;
            }
            string name = cbDevType.Text;
            DevType curType = LimitSetConfig.DevTypes.FirstOrDefault( n => n.Name == name );
            if ( curType != null )
            {
                curType.MeteUpper = meteUpper.ToString( );
                curType.MeteLow = meteLow.ToString( );
                curType.AlertUpper = alertUpper.ToString( );
                curType.AlertLow = alertLow.ToString( );
                curType.CustomSetEnabled = ckEnable.Checked;
                curType.Alias = txtAlias.Text;
            }

            LimitSetConfigurationManager.SubmitChanges( );

            MessageBox.Show( string.Format( "[{0}]设置成功，需要重启软件才能生效！" , name ) );
        }

        private void cbDevType_SelectedIndexChanged( object sender , EventArgs e )
        {
            txtLCSX.Text = string.Empty;
            txtLCXX.Text = string.Empty;
            txtBJSX.Text = string.Empty;
            txtBJXX.Text = string.Empty;
            txtAlias.Text = string.Empty;
            ckEnable.Checked = false;
            string name = cbDevType.Text;
            DevType curType = LimitSetConfig.DevTypes.FirstOrDefault( n => n.Name == name );
            if ( curType != null )
            {
                txtLCSX.Text = curType.MeteUpper;
                txtLCXX.Text = curType.MeteLow;
                txtBJSX.Text = curType.AlertUpper;
                txtBJXX.Text = curType.AlertLow;
                ckEnable.Checked = curType.CustomSetEnabled;
                txtAlias.Text = curType.Alias;
            }
        }

        private void btnSaveLimitSet2_Click( object sender , EventArgs e )
        {
            if ( string.IsNullOrEmpty( cbDevID.Text ) )
            {
                MessageBox.Show( "请选择或者输入测点编号！" );
                return;
            }
            BaseCoalMineUnit coalMineUnit = CoalMineUnitManager.Instance( ).GetCoalMineUnit( KWBridgeConfig.Instance.Id );
            if ( coalMineUnit == null || coalMineUnit.GetNoDeletesMonitoringUnits( ).FirstOrDefault( n => n.m_strId == cbDevID.Text ) == null )
            {
                MessageBox.Show( "无效的测点编号！" );
                return;
            }
            float meteUpper = 0f , meteLow = 0f , alertUpper = 0f , alertLow = 0f;
            if ( !string.IsNullOrEmpty( txtLCXX2.Text ) && !float.TryParse( txtLCSX2.Text , out  meteUpper ) )
            {
                MessageBox.Show( "量程上限仅支持浮点类型，请重新输入！" );
                txtLCSX2.Focus( );
                return;
            }
            if ( !string.IsNullOrEmpty( txtLCXX2.Text ) && !float.TryParse( txtLCXX2.Text , out  meteLow ) )
            {
                MessageBox.Show( "量程下限仅支持浮点类型，请重新输入！" );
                txtLCXX2.Focus( );
                return;
            }
            if ( !string.IsNullOrEmpty( txtBJSX2.Text ) && !float.TryParse( txtBJSX2.Text , out  alertUpper ) )
            {
                MessageBox.Show( "报警上限仅支持浮点类型，请重新输入！" );
                txtBJSX2.Focus( );
                return;
            }
            if ( !string.IsNullOrEmpty( txtBJXX2.Text ) && !float.TryParse( txtBJXX2.Text , out  alertLow ) )
            {
                MessageBox.Show( "报警下限仅支持浮点类型，请重新输入！" );
                txtBJXX2.Focus( );
                return;
            }
            string id = cbDevID.Text;
            Dev curDev = LimitSetConfig.Devs.FirstOrDefault( n => n.ID == id );
            if ( curDev == null )
            {
                curDev = new Dev( );
                LimitSetConfig.Devs.Add( curDev );
            }

            curDev.ID = id;
            curDev.MeteUpper = meteUpper.ToString( );
            curDev.MeteLow = meteLow.ToString( );
            curDev.AlertUpper = alertUpper.ToString( );
            curDev.AlertLow = alertLow.ToString( );
            curDev.CustomSetEnabled = ckEnable2.Checked;
            if ( cbDevType2.SelectedIndex > -1 )
                curDev.DevType = cbDevType2.Text;
            LimitSetConfigurationManager.SubmitChanges( );

            MessageBox.Show( string.Format( "[{0}]设置成功，需要重启软件才能生效！" , id ) );
        }

        private void cbDevID_SelectedIndexChanged( object sender , EventArgs e )
        {
            txtLCSX2.Text = string.Empty;
            txtLCXX2.Text = string.Empty;
            txtBJSX2.Text = string.Empty;
            txtBJXX2.Text = string.Empty;
            ckEnable2.Checked = false;
            cbDevType2.SelectedIndex = -1;
            string id = cbDevID.Text;
            Dev curDev = LimitSetConfig.Devs.FirstOrDefault( n => n.ID == id );
            if ( curDev != null )
            {
                txtLCSX2.Text = curDev.MeteUpper;
                txtLCXX2.Text = curDev.MeteLow;
                txtBJSX2.Text = curDev.AlertUpper;
                txtBJXX2.Text = curDev.AlertLow;
                ckEnable2.Checked = curDev.CustomSetEnabled;
                cbDevType2.Text = curDev.DevType;
            }
        }

        private void cbDevID_Leave( object sender , EventArgs e )
        {
            if ( string.IsNullOrEmpty( cbDevID.Text ) )
                return;
            BaseCoalMineUnit coalMineUnit = CoalMineUnitManager.Instance( ).GetCoalMineUnit( KWBridgeConfig.Instance.Id );
            if ( coalMineUnit == null || coalMineUnit.GetNoDeletesMonitoringUnits( ).FirstOrDefault( n => n.m_strId == cbDevID.Text ) == null )
            {
                MessageBox.Show( "无效的测点编号！" );
                return;
            }
            cbDevID_SelectedIndexChanged( null , null );
        }

        private void btnSaveUnit_Click( object sender , EventArgs e )
        {
            if ( string.IsNullOrEmpty( cbCurUnit.Text ) )
            {
                MessageBox.Show( "请选择要转换单位！" );
                cbCurUnit.Select( );
                return;
            }
            Unit curUnit = LimitSetConfig.Units.FirstOrDefault( n => n.From == cbCurUnit.Text );
            if ( curUnit == null )
            {
                curUnit = new Unit( );
                LimitSetConfig.Units.Add( curUnit );
            }

            curUnit.From = cbCurUnit.Text;
            curUnit.To = txtNewUnit.Text;

            LimitSetConfigurationManager.SubmitChanges( );
            MessageBox.Show( "设置成功，需要重启软件才能生效！" );
        }

        private void cbCurUnit_SelectedIndexChanged( object sender , EventArgs e )
        {
            txtNewUnit.Text = string.Empty;
            string unit = cbCurUnit.Text;
            Unit curUnit = LimitSetConfig.Units.FirstOrDefault( n => n.From == unit );
            if ( curUnit != null )
            {
                txtNewUnit.Text = curUnit.To;
            }
        }
    }
}